package com.syni.mdd.yhd.common.all.controller.api.order;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.sjb.common.component.utils.common.utils.DateTimeUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.common.component.utils.common.utils.RpcResponseVo;
import com.syni.mdd.sjb.common.component.utils.common.utils.StringUtils;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.dto.reponseDto.order.BusinessOrderDetailRpDto;
import com.syni.mdd.yhd.common.all.dto.reponseDto.order.OrderManagerList;
import com.syni.mdd.yhd.common.all.enums.groupBuy.BargainGroupBuyEnums;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddAll.apiOrder.FindByOrderIdInDto;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddAll.apiOrder.GetByBusinessOrderForDataBankDto;
import com.syni.mdd.sjb.common.component.utils.feign.service.mddAll.OrderRestService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.Primary;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListMap;

/**
 * @className OrderRestApi
 * @description TOO
 * @Author cfx
 * @DATE 2020/3/18 10:06
 * @VERSION 1.0
 **/
@RestController
@RequestMapping("/apiOrder")
@Log4j2
public class OrderRestApi implements OrderRestService {

    @Autowired
    OrderRestApiService orderRestApiService;

    @GetMapping("/timeToCancel")
    public RpcResponseVo timeToCancel(@RequestParam("orderId") Integer orderId,
                                      @RequestParam("cancelTime") String cancelTime){
        return orderRestApiService.timeToCancel(orderId,cancelTime);
    }

    @PostMapping("/getByOrderIds")
    public RpcResponseVo findByOrderIdIn(@RequestBody FindByOrderIdInDto findByOrderIdInDto) {
        Pageable pageable = null;
        if(StringUtils.equals(findByOrderIdInDto.getSort(),"DESC")){
            pageable = PageRequest.of(findByOrderIdInDto.getPageNum() - 1, findByOrderIdInDto.getPageSize(), Sort.Direction.DESC, findByOrderIdInDto.getOrderColumn());
        }else if(StringUtils.equals(findByOrderIdInDto.getSort(),"ASC")){
            pageable = PageRequest.of(findByOrderIdInDto.getPageNum() - 1, findByOrderIdInDto.getPageSize(), Sort.Direction.DESC, findByOrderIdInDto.getOrderColumn());
        }else{
            return RpcResponseVo.result("排序出错", ResultCode.CODE_ERROR_100);
        }
        return orderRestApiService.findByOrderIdIn(findByOrderIdInDto, pageable);
    }

    @PostMapping("/getByBusinessOrderForDataBank")
    public RpcResponseVo getByBusinessOrderForDataBank(@RequestBody GetByBusinessOrderForDataBankDto getByBusinessOrderForDataBankDto) {
        final int MDD_GROUP_TYP_MATCH = 1;
        final int MDD_YOUHUI_MAI_DAN_MATCH = 2;
        Map<String,Object> retMap = orderRestApiService.getByBusinessOrderForDataBank(getByBusinessOrderForDataBankDto);
        //逻辑
        if(!ResultMapHelper.isSuccess(retMap)){
            return RpcResponseVo.result(ResultCode.CODE_ERROR, ResultMsg.HAND_FAIL);
        }
        BusinessOrderDetailRpDto businessOrderDetailRpDto = (BusinessOrderDetailRpDto) retMap.get("data");
        getByBusinessOrderForDataBankDto.setCount(businessOrderDetailRpDto.getCount());
        List<OrderManagerList> orders = businessOrderDetailRpDto.getOrderManagerLists();
        //log.info("orders：{}" + JSON.toJSONString(orders));
        if(CollectionUtils.isNotEmpty(orders)){
            for(OrderManagerList orderManagerList : orders){
                GetByBusinessOrderForDataBankDto.MddOrderData mddOrderData = new GetByBusinessOrderForDataBankDto.MddOrderData();
                mddOrderData.setActualConsum(orderManagerList.getShopConsum());
                if(orderManagerList.getOrderType() == Constants.GROUP_BUY_ORDER_TYPE
                || orderManagerList.getOrderType() == Constants.GROUP_BUY_BARGAIN_ORDER_TYPE
                || orderManagerList.getOrderType() == Constants.FX_ORDER_TYPE){
                    mddOrderData.setOrderType(MDD_GROUP_TYP_MATCH);
                    mddOrderData.setGroupName(orderManagerList.getGroupName());
                    mddOrderData.setVerTime(DateTimeUtils.converDateToLocalDateTime(orderManagerList.getUseTime()));
                    mddOrderData.setVerWay(orderManagerList.getUseWay());
                }else if(orderManagerList.getOrderType() == Constants.ONLINE_ORDER_TYPE
                        || orderManagerList.getOrderType() == Constants.COUPONS_ORDER_TYPE){
                    mddOrderData.setOrderType(MDD_YOUHUI_MAI_DAN_MATCH);
                    if(orderManagerList.getOrderType() == Constants.ONLINE_ORDER_TYPE){
                        mddOrderData.setCouponType(Constants.COUPON_ONLINE);
                    }else if(orderManagerList.getOrderType() == Constants.COUPONS_ORDER_TYPE){
                        mddOrderData.setCouponType(Constants.COUPON_SET);
                    }
                    mddOrderData.setActualConsum(orderManagerList.getActualConsum());
                    mddOrderData.setPayContent(orderManagerList.getOrderManagerDetail().getPayContent());
                    mddOrderData.setPayTime(DateTimeUtils.converDateToLocalDateTime(orderManagerList.getPayTime()));
                }
                getByBusinessOrderForDataBankDto.getOrders().add(mddOrderData);
            }
        }
        return RpcResponseVo.success(getByBusinessOrderForDataBankDto,ResultMsg.MSG_GET_SUCCESS);

    }

}
